//
//  HWInfoDetailAllCommitListVC.swift
//  HWanGame
//
//  Created by Apple on 2024/8/17.
//  Copyright © 2024 94Wan. All rights reserved.
//

import UIKit


class HWInfoDetailAllCommitListVC: HWBaseViewController {

    private lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = .clear
        tableView.showsVerticalScrollIndicator = false
        tableView.register(HWCommitCell.self)
        tableView.contentInsetAdjustmentBehavior = .never
        tableView.separatorStyle = .none
        tableView.estimatedRowHeight = 120
        return tableView
    }()
    var gid = ""
    var infoId = 0
    private var httpManager = HWHomeHttpManager()
    private var commitIndex = 1
    
    var dataSource = [HWInfoCommentListModel]()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        navigationView.titleLabel.text = "全部评论"
        navigationView.backButton.setImage(UIImage(named: "navBackButton"), for: .normal)
        view.backgroundColor = "#F7F8FA".toColor
        
        loadNewData()
        addRefresh()
        
        view.subviews{
            tableView
        }
        
        tableView.leading(0).trailing(0).bottom(0).top(.navi_height)
    }
    

}

extension HWInfoDetailAllCommitListVC: UITableViewDelegate, UITableViewDataSource {
    
//    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//        //跳转到详情
//        let sonController = HWGameCommentSonController()
//        sonController.id = dataSource[0].id
//        sonController.gameId = self.gid
//        sonController.releaseCallBack = {
//            self.loadData()
//        }
//        self.navigationController?.pushViewController(sonController, animated: true)
//    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return dataSource.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "HWCommitCell", for: indexPath) as! HWCommitCell
        cell.commitModel = dataSource[indexPath.row]
        return cell
    }
}

//MARK: Data
extension HWInfoDetailAllCommitListVC {
    
    private func addRefresh() {
        
        tableView.mj_header = MJRefreshNormalHeader()
        tableView.mj_header?.setRefreshingTarget(self, refreshingAction: #selector(self.loadNewData))
        tableView.mj_footer = MJRefreshFooter()
        tableView.mj_footer.setRefreshingTarget(self, refreshingAction: #selector(loadMoreData))
    }
    
    private func loadData(){
        self.httpManager.getInfoCommitList(infoId: "\(infoId)", p: commitIndex) { models in
            self.tableView.mj_header?.endRefreshing()
            self.tableView.mj_footer?.endRefreshing()
            self.dataSource.removeAll()
            self.dataSource = models
            self.tableView.reloadData()
        } failure: { error in
            self.view.showHUD(title: error)
        }
    }
    
    @objc private func loadNewData() {
        self.commitIndex = 1
        loadData()
    }
    
    @objc private func loadMoreData(){
        self.commitIndex += 1
        loadData()
    }
    
    private func loadMoreCommitListData() {
        self.tableView.mj_footer?.endRefreshing()
        self.httpManager.getInfoCommitList(infoId: "\(infoId)", p: commitIndex) { models in
            self.tableView.mj_footer?.endRefreshing()
            self.dataSource.append(contentsOf: models)
            self.tableView.reloadData()
        } failure: { error in
            self.view.showHUD(title: error)
            self.tableView.mj_footer?.endRefreshing()
        }
    }
}
